#include "my_adc_drv.h"
#include "my_uart.h"
/**********
 *  // 眼部模式插入检测，未插入时为低电平 -----------
 * 
PA0--------BAT_ADC      // vat---10k---adc_detect---10k---GND-----[ADC_IN1]
PA3-------NTC4_ADC      //电极头温度4  NTC 10K ， VCC_3V0---10K---ntc_check----NTC_10K---GND ---[ADC_IN4]
PA5-------NTC1_ADC      // 电极头温度1                        ---------------------[ADC_IN6]
PA6-------NTC2_ADC      // 电极头温度2 -[ADC_IN7]
PA7-------NTC3_ADC      //电极头温度3 眼部模式插入检测，未插入时为低电平 ------------[ADC_IN8]


PB0------IHF_ADC     // 高周波--负载检测  ---------[ADC_IN9]
PB1------HF_VCC_ADC  // 高周波电压 检测   ---------[ADC_IN10]
 * 
 * 
*/

#define READ_ADC_NUM        (5)

static ADC_InitType ADC_InitStructure;
static DMA_InitType DMA_InitStructure;
static __IO uint16_t ADCConvertedValue[READ_ADC_NUM * 2];

/**
 * @brief  Configures the different system clocks.
 */
static void adc_rcc_cfg(void)
{
    /* Enable peripheral clocks ------------------------------------------------*/
    /* Enable DMA clocks */
    RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA, ENABLE);

    /* Enable GPIOC clocks */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA |RCC_APB2_PERIPH_GPIOB|RCC_APB2_PERIPH_AFIO, ENABLE);
    /* Enable ADC clocks */
    RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);

    /* RCC_ADCHCLK_DIV16*/
    ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV16);
   RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSI, RCC_ADC1MCLK_DIV8);
}

/**
 * @brief  Configures the different GPIO ports.
 */
static void adc_gpio_cfg(void)
{
    GPIO_InitType GPIO_InitStructure;

    GPIO_InitStruct(&GPIO_InitStructure);
    /* Configure PC.02 (in12)as analog input -------------------------*/
    GPIO_InitStructure.Pin       = GPIO_PIN_0|GPIO_PIN_5|GPIO_PIN_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Analog;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

        /* Configure PC.02 (in12)as analog input -------------------------*/
    GPIO_InitStructure.Pin       = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Analog;
    GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}

static void adc_dma_cfg(void)
{
    /* DMA channel1 configuration ----------------------------------------------*/
    DMA_DeInit(DMA_CH1);
    DMA_InitStructure.PeriphAddr     = (uint32_t)&ADC->DAT;
    DMA_InitStructure.MemAddr        = (uint32_t)ADCConvertedValue;
    DMA_InitStructure.Direction      = DMA_DIR_PERIPH_SRC;
    DMA_InitStructure.BufSize        = READ_ADC_NUM;
    DMA_InitStructure.PeriphInc      = DMA_PERIPH_INC_DISABLE;
    DMA_InitStructure.DMA_MemoryInc  = DMA_MEM_INC_ENABLE;
    DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
    DMA_InitStructure.MemDataSize    = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.CircularMode   = DMA_MODE_CIRCULAR;
    DMA_InitStructure.Priority       = DMA_PRIORITY_HIGH;
    DMA_InitStructure.Mem2Mem        = DMA_M2M_DISABLE;
    DMA_Init(DMA_CH1, &DMA_InitStructure);
    DMA_RequestRemap(DMA_REMAP_ADC1, DMA, DMA_CH1, ENABLE);
   /* Enable DMA channel1 */
    DMA_EnableChannel(DMA_CH1, ENABLE);
}

static void adc_cfg(void)
{

    ADC_DeInit(ADC);
    /* ADC1 configuration ------------------------------------------------------*/
    ADC_InitStructure.MultiChEn      = ENABLE;
    ADC_InitStructure.ContinueConvEn = ENABLE;
    ADC_InitStructure.ExtTrigSelect  = ADC_EXT_TRIGCONV_NONE;
    ADC_InitStructure.DatAlign       = ADC_DAT_ALIGN_R;
    ADC_InitStructure.ChsNumber      = READ_ADC_NUM;
    ADC_Init(ADC, &ADC_InitStructure);
    /* ADC1 regular channel13 configuration */
    ADC_ConfigRegularChannel(ADC, ADC_CH_1_PA0, 1, ADC_SAMP_TIME_71CYCLES5);
    // ADC_ConfigRegularChannel(ADC, ADC_CH_4_PA3, 2, ADC_SAMP_TIME_71CYCLES5);
    ADC_ConfigRegularChannel(ADC, ADC_CH_6_PA5, 2, ADC_SAMP_TIME_71CYCLES5);


    ADC_ConfigRegularChannel(ADC, ADC_CH_7_PA6, 3, ADC_SAMP_TIME_71CYCLES5);
    // ADC_ConfigRegularChannel(ADC, ADC_CH_8_PA7, 5, ADC_SAMP_TIME_71CYCLES5);

    ADC_ConfigRegularChannel(ADC, ADC_CH_9_PB0, 4, ADC_SAMP_TIME_71CYCLES5);
    ADC_ConfigRegularChannel(ADC, ADC_CH_10_PB1, 5, ADC_SAMP_TIME_71CYCLES5);
    /* Enable ADC DMA */
    ADC_EnableDMA(ADC, ENABLE);

    /* Enable ADC */
    ADC_Enable(ADC, ENABLE);
    /* Check ADC Ready */
    while(ADC_GetFlagStatusNew(ADC,ADC_FLAG_RDY) == RESET)
        ;
    /* Start ADC1 calibration */
    ADC_StartCalibration(ADC);
    /* Check the end of ADC1 calibration */
    while (ADC_GetCalibrationStatus(ADC))
        ;

    /* Start ADC1 Software Conversion */
    ADC_EnableSoftwareStartConv(ADC, ENABLE);
}

void my_adc_data_dump(void)
{
#ifdef DEBUG_OPEN_PRINT    
    printf("BAT_vol adc_pa0:[%d]\r\n", ADCConvertedValue[0]);
    printf(" ntc4 adc_pa3:[%d]\r\n", ADCConvertedValue[1]);
    
    printf("ntc1 adc_pa5:[%d]\r\n", ADCConvertedValue[2]);
    printf("ntc2 adc_pa6:[%d]\r\n", ADCConvertedValue[3]);
    printf("ntc3 adc_pa7:[%d]\r\n", ADCConvertedValue[4]);

    printf("HF_load adc_pb0:[%d]\r\n", ADCConvertedValue[5]);
    printf("HF_VCC adc_pb1:[%d]\r\n", ADCConvertedValue[6]);
#endif    
}

/***********************************/
// NTC读取
/***********************************/
uint16_t my_NTC1_adc_read(void)
{
    return ADCConvertedValue[1];
}

uint16_t my_NTC2_adc_read(void)
{
    return ADCConvertedValue[2];
}

uint16_t my_NTC3_adc_read(void)
{
    return ADCConvertedValue[1];
}

uint16_t my_NTC4_adc_read(void)
{
    return ADCConvertedValue[2];
}
/***********************************/
/***********************************/

uint16_t my_HF_load_adc_read(void)
{
    return  ADCConvertedValue[3];
}

uint16_t my_HF_vcc_adc_read(void)
{
    return  ADCConvertedValue[4];
}



uint16_t my_battery_adc_read(void)
{
    return  ADCConvertedValue[0];
}


void my_adc_init(void)
{
    adc_rcc_cfg();
    adc_gpio_cfg();
    adc_dma_cfg();
    adc_cfg();
}